<!-- 处方编辑弹窗 -->

<template>
  <el-dialog :title="isUpdate ? '修改处方' : '添加处方'" :visible="visible" width="750px" :destroy-on-close="true"
    :lock-scroll="false" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="处方图:" prop="problem_img">
            <uploadImage :limit="1" v-model="form.problem_img"></uploadImage>
          </el-form-item>
          <el-form-item label="所属题库:" prop="topic_id">
            <el-select v-model="form.topic_id" clearable filterable placeholder="请选择所属题库" @change="topicOptionChange">
              <el-option v-for="item in topicOption" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="处方名称:" prop="title">
            <el-input clearable :maxlength="150" v-model="form.title" placeholder="请输入处方名称" />
          </el-form-item>
          <el-form-item label="处方状态:" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'PrescriptionEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { uploadImage },
  data() {
    return {
      topicOption: [],
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      // 表单验证规则
      rules: {
        topic_id: [
          { required: true, message: '请选择所属题库', trigger: 'blur' }
        ],
        problem_img: [
          { required: true, message: '请输入处方图', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入处方名称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择处方状态', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' }
        ]
      },

      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        console.log(this.data)
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  // 页面创建时
  created() {
    this.topicList();
  },

  methods: {
    topicOptionChange(){
      this.form.title=''
    },
    /* 获取题库列表 */
    topicList() {
      this.$http.get('/topic/getTopicSelectList').then(res => {
        if (res.data.code === 0) {
          this.topicOption = res.data.data;
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/prescription/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped></style>
